﻿using IFRS.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace IFRS.DataAccess.DAL
{
    public class SPR_INSERTNDIVIDUALIMPAIR
    {
        public DataTable SEARCH_ALL(IndividualImpair individualImpair, Paging paging, bool IFRS_ALLBRANCH, string sIFRS_COST_CENTER, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", IFRS_ALLBRANCH));
                lstParams.Add(new SqlParameter("@sCostCenter", sIFRS_COST_CENTER));

                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_INSERTNDIVIDUALIMPAIR_SEARCHALL", lstParams.ToArray());

                iRecordCount = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int IndividualImpairID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", IndividualImpairID));
                dt = Utility.StoredProcDataTable("SPR_INSERTNDIVIDUALIMPAIR_SEARCHBYID", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(IndividualImpair individualImpair)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate));
                lstParams.Add(new SqlParameter("@IndividualImpairAmount", individualImpair.IndividualImpairAmount));
                lstParams.Add(new SqlParameter("@CollateralAmount", individualImpair.CollateralAmount));
                lstParams.Add(new SqlParameter("@InterestRate", individualImpair.InterestRate));
                lstParams.Add(new SqlParameter("@IsConfirmed", individualImpair.IsConfirmed));
                lstParams.Add(new SqlParameter("@CreatedDate", individualImpair.CreatedDate));
                lstParams.Add(new SqlParameter("@CreatedBy", individualImpair.CreatedBy));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", individualImpair.CreatedCostCenter));
                lstParams.Add(new SqlParameter("@CreatedBranch", individualImpair.CreatedBranch));
                lstParams.Add(new SqlParameter("@UpdatedDate", individualImpair.UpdatedDate));
                lstParams.Add(new SqlParameter("@UpdatedBy", individualImpair.UpdatedBy));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", individualImpair.UpdatedCostCenter));
                lstParams.Add(new SqlParameter("@UpdatedBranch", individualImpair.UpdatedBranch));

                Utility.ExecuteStoredProc("SPR_INSERTNDIVIDUALIMPAIR_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(IndividualImpair individualImpair)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", individualImpair.IndividualImpairID));
                lstParams.Add(new SqlParameter("@TransactionDate", individualImpair.TransactionDate));
                lstParams.Add(new SqlParameter("@SourceDataID", individualImpair.SourceDataID));
                lstParams.Add(new SqlParameter("@AccountNo", individualImpair.AccountNo));
                lstParams.Add(new SqlParameter("@PaymentDate", individualImpair.PaymentDate));
                lstParams.Add(new SqlParameter("@IndividualImpairAmount", individualImpair.IndividualImpairAmount));
                lstParams.Add(new SqlParameter("@CollateralAmount", individualImpair.CollateralAmount));
                lstParams.Add(new SqlParameter("@InterestRate", individualImpair.InterestRate));
                lstParams.Add(new SqlParameter("@IsConfirmed", individualImpair.IsConfirmed));
                lstParams.Add(new SqlParameter("@UpdatedDate", individualImpair.UpdatedDate));
                lstParams.Add(new SqlParameter("@UpdatedBy", individualImpair.UpdatedBy));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", individualImpair.UpdatedCostCenter));
                lstParams.Add(new SqlParameter("@UpdatedBranch", individualImpair.UpdatedBranch));

                iNum = (int)Utility.ExecuteStoredProc("SPR_INSERTNDIVIDUALIMPAIR_UPDATE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE_STATUS(int IndividualImpairID)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", IndividualImpairID));
                iNum = (int)Utility.ExecuteStoredProc("[SPR_INSERTNDIVIDUALIMPAIR_UPDATE_ISCONFIRMED]", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(int IndividualImpairID)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@IndividualImpairID", IndividualImpairID));
                iNum = (int)Utility.ExecuteStoredProc("SPR_INSERTNDIVIDUALIMPAIR_DELETE", lstParams.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }
    }
}